/**
 * 
 */
package marpg;

import marpg.core.Agent;
import marpg.network.ConnectionHandler;

/**
 * @author Steven
 * 
 */
public class Boot {
	// The default agent class name
	private static final String DEFAULT_AGENT_CLASS_NAME = "marpg.core.SimpleAgent";

	// The default port and host
	private static final String DEFAULT_HOST = "localhost";
	private static final int DEFAULT_PORT_NUMBER = 2022;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String agentClassName;
		String host;
		int portNumber;

		if (args.length == 0) {
			agentClassName = DEFAULT_AGENT_CLASS_NAME;
			System.out.println("Using default agent=" + agentClassName);
		} else {
			agentClassName = args[0];
		}

		if (args.length < 3) {
			host = DEFAULT_HOST;
			portNumber = DEFAULT_PORT_NUMBER;
			System.out.println("Using default host=" + host + ", portNumber=" + portNumber);
		} else {
			host = args[1];
			portNumber = Integer.valueOf(args[2]).intValue();
		}

		try {
			Class<?> agentClass = Class.forName(agentClassName);
			Agent agent = (Agent) agentClass.newInstance();
			agent.setup();

			ConnectionHandler connectionHandler = new ConnectionHandler();
			agent.setConnectionHandler(connectionHandler);

			connectionHandler.setup(agent, host, portNumber);
			connectionHandler.connect();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}

	}
}
